package com.vito.util.oss;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import com.vito.config.AppConfig;

import java.io.File;
import java.io.InputStream;

public class COSUtil {
	
	static COSCredentials cred = null;
	static ClientConfig clientConfig = null;
	
	/**
	 * 上传文件
	 */
	public static void upload(String file,String bucketName,String dest){
		if(cred==null) {
			cred = new BasicCOSCredentials(AppConfig.OSS_SECRETID, AppConfig.OSS_SECRETKEY);
		}
		if(clientConfig==null) {
			clientConfig = new ClientConfig(new Region(AppConfig.OSS_REGION));
		}
		COSClient cosClient = null;
		try {
			cosClient = new COSClient(cred, clientConfig);
			// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
			// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
			// 大文件上传请参照 API 文档高级 API 上传
			File localFile = new File(file);
			// 指定要上传到 COS 上的路径
			PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dest, localFile);
//			PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
			cosClient.putObject(putObjectRequest);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cosClient.shutdown();
		}
		
	}
	/**
	 * 上传文件
	 */
	public static void upload(File file,String bucketName,String dest){
		COSClient cosClient = null;
		if(cred==null) {
			cred = new BasicCOSCredentials(AppConfig.OSS_SECRETID, AppConfig.OSS_SECRETKEY);
		}
		if(clientConfig==null) {
			clientConfig = new ClientConfig(new Region(AppConfig.OSS_REGION));
		}
		try {
			cosClient = new COSClient(cred, clientConfig);
			// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
			// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
			// 大文件上传请参照 API 文档高级 API 上传
			// 指定要上传到 COS 上的路径
			PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dest, file);
			cosClient.putObject(putObjectRequest);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cosClient.shutdown();
		}
		
	}
	
	/**
	 * 上传文件
	 */
	public static void upload(InputStream in,String bucketName,String dest,ObjectMetadata metadata){
		COSClient cosClient = null;
		if(cred==null) {
			cred = new BasicCOSCredentials(AppConfig.OSS_SECRETID, AppConfig.OSS_SECRETKEY);
		}
		if(clientConfig==null) {
			clientConfig = new ClientConfig(new Region(AppConfig.OSS_REGION));
		}
		try {
			cosClient = new COSClient(cred, clientConfig);
			// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
			// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
			// 大文件上传请参照 API 文档高级 API 上传
			// 指定要上传到 COS 上的路径
			/*ObjectMetadata metadata = new ObjectMetadata();
			metadata.setContentType("image/jpeg");*/
			PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, dest, in, metadata);
			cosClient.putObject(putObjectRequest);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cosClient.shutdown();
		}
		
	}	
	/**
	 * 删除文件
	 * @param key
	 * @param bucketName
	 */
	public static void delete(String key,String bucketName){
		if(cred==null) {
			cred = new BasicCOSCredentials(AppConfig.OSS_SECRETID, AppConfig.OSS_SECRETKEY);
		}
		if(clientConfig==null) {
			clientConfig = new ClientConfig(new Region(AppConfig.OSS_REGION));
		}
		COSClient cosClient = new COSClient(cred, clientConfig);
		cosClient.deleteObject(bucketName, key);
	}
	
	/**
	 * 创建bucket
	 * @param bucketName
	 */
	public static void createBucket(String bucketName){
		COSClient cosClient = null;
		if(cred==null) {
			cred = new BasicCOSCredentials(AppConfig.OSS_SECRETID, AppConfig.OSS_SECRETKEY);
		}
		if(clientConfig==null) {
			clientConfig = new ClientConfig(new Region(AppConfig.OSS_REGION));
		}
		try {
			cosClient = new COSClient(cred, clientConfig);
			cosClient.createBucket(bucketName + "-" + AppConfig.OSS_APPID);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cosClient.shutdown();
		}
	}
}
